package com.huarui.part.action;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;

import com.huarui.part.model.PartUpload;
import com.huarui.part.service.PartUploadService;
import com.huarui.part.util.PartOperateResultEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class PartUploadEditAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5003900559350396246L;

	private static final Logger logger = Logger
			.getLogger(PartUploadEditAction.class);

	private PartUploadService partUploadService;

	private Map<String, Object> resultMap;

	private String jsonData;

	public String updatePartUpload() {

		PartUpload partUpload = (PartUpload) JSONObject.toBean(
				JSONObject.fromObject(jsonData), PartUpload.class);
		SessionMessage sm = UserHolder.getCurrentSessionMessage();

		PartUpload p = partUploadService.getPartUploadById(partUpload.getId());
		if (p != null) {

			if (p.getStatus().equals(PartUpload.STATUS_CREATE)
					&& p.getCompanyId().equals(sm.getCompanyId())) {
				partUpload.setCompanyId(sm.getCompanyId());
				partUpload.setCompanyName(sm.getCompanyName());
				partUpload.setCode(partUpload.getCode().trim());
				partUpload.setBrand(partUpload.getBrand().trim());
				partUpload.setCarModel(partUpload.getCarModel().trim());
				partUpload.setName(partUpload.getName().trim());
				partUpload.setNameEn(partUpload.getNameEn().trim());
				resultMap = partUploadService.upadte(partUpload);
			} else {
				resultMap = new HashMap<String, Object>();
				resultMap.put(PartOperateResultEnum.SUCCESS.getMsg(), false);
				resultMap.put("msg",
						PartOperateResultEnum.RROR_MSG_STATES_CANNOTMODI
								.getMsg());
			}

		} else {

			resultMap = new HashMap<String, Object>();
			resultMap.put(PartOperateResultEnum.SUCCESS.getMsg(), false);
			resultMap.put("msg",
					PartOperateResultEnum.RROR_MSG_RECORD_NONE.getMsg());
		}

		return JSON;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public String getJsonData() {
		return jsonData;
	}

	public void setJsonData(String jsonData) {
		this.jsonData = jsonData;
	}

	public void setPartUploadService(PartUploadService partUploadService) {
		this.partUploadService = partUploadService;
	}

}
